//
// Created by Jisam on 2024/8/19 16:28.
// Solution of  Lake Counting
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

using namespace std;

#define endl "\n"
#define PSI pair<string,int>
#define PII pair<int,int>
#define PDI pair<double,int>
#define PDD pair<double,double>
#define VVI vector<vector<int>>
#define VI vector<int>
#define VS vector<string>

#define PQLI priority_queue<int, vector<int>, less<int>>
#define PQGI priority_queue<int, vector<int>, greater<int>>
#define code_by_jisam ios::sync_with_stdio(false),cin.tie(nullptr)
using namespace std;
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
int dx[] = {-1, 1, 0, 0, 1, 1, -1, -1,};
int dy[] = {0, 0, -1, 1, 1, -1, -1, 1,};
int ans = 0;

void bfs(int x, int y, int n, int m, VS &a, VVI &vis,char ch) {
    ans++;
//    cout << ans << " ================= " << endl;
    queue<PII > q;
    q.push(make_pair(x, y));
    vis[x][y] = ans;
    while (!q.empty()) {
        auto [x, y] = q.front();
        q.pop();

        if (x == n - 1 && y == m - 1) {
            continue;
        }
        for (int i = 0; i < 8; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];

            if (tx >= 0 && ty >= 0 && tx < n && ty < m && vis[tx][ty] == 0 && a[tx][ty] == ch) {
                q.push(make_pair(tx, ty));
                vis[tx][ty] = ans;
//                cout << tx << " " << ty << endl;
            }
        }
    }
}

void solution() {
    int n, m;
    cin >> n >> m;
    VS a(n);
    VVI vis(n, vector<int>(m, 0));
    for (int i = 0; i < n; i++) cin >> a[i];
    char ch = 'W';
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (vis[i][j] == 0 && a[i][j] == ch) {
//                cout << i << " " << j << "++++++++++++++++++++++" <<endl;
                bfs(i, j, n, m, a, vis,ch);
            }
        }
    }
    cout << ans << endl;
}

int main() {
    code_by_jisam;
    int T = 1;
//    cin >> T;
    while (T--) {
        solution();
    }
    return 0;
}